import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'location/permissions_options.dart';



/// 如有需要添加新的权限，请添加至此集合
enum PermissionType { photo, camera, location, storage, notification, accessMediaLocation, microphone,calendar }

Map<PermissionType, _PermissionItem> _permissionsMap = {
  PermissionType.photo:
      _PermissionItem(androidPermission: Permission.storage, iosPermission: Permission.photos, hint: "您现在无法使用相册功能，请开启相册/存储权限"),
  PermissionType.camera:
      _PermissionItem(androidPermission: Permission.camera, iosPermission: Permission.camera, hint: "您现在无法使用拍摄功能，请开启拍摄权限"),
  PermissionType.location:
      _PermissionItem(androidPermission: Permission.location, iosPermission: Permission.locationWhenInUse, hint: "您现在无法使用定位功能，请开启定位权限"),
  PermissionType.storage:
      _PermissionItem(androidPermission: Permission.storage, iosPermission: Permission.storage, hint: "您现在无法使用存储功能，请开启存储权限"),
  PermissionType.notification: _PermissionItem(
      androidPermission: Permission.notification, iosPermission: Permission.notification, hint: "您现在无法使用推送功能，请开启通知权限"),
  PermissionType.microphone: _PermissionItem(
      androidPermission: Permission.microphone, iosPermission: Permission.microphone, hint: "您现在无法使用发送语音消息功能，请开启麦克风权限"),
  PermissionType.accessMediaLocation: _PermissionItem(
      androidPermission: Permission.accessMediaLocation,
      iosPermission: Permission.accessMediaLocation,
      hint: "您现在无法使用相册功能，请开启通知权限"),
  PermissionType.calendar:
  _PermissionItem(androidPermission: Permission.calendar, iosPermission: Permission.calendar, hint: "您现在无法使用日历功能，请开启日历权限"),
};

class PermissionsUtils {
  /// 参考flutter-permission-handler此issue
  /// https://github.com/Baseflow/flutter-permission-handler/issues/644
  ///
  /// 关于用户拒绝权限后跳转到设置页，从设置页回来后的回调问题
  /// 如果没有这方面的要求，比如需要点击才请求的权限，那么让用户重新点击一次即可
  /// 如果对这方面有要求，从设置页回来后必须立即做出一些操作，建议参考example的permissions_demo，通过with WidgetsBindingObserver来实现
  /// andorid: 修改回来后可以直接拿到最新的权限
  /// ios：修改回来后app会自动重启，无需做别的操作
  static bool isOpenSetting = false;

  static Future<bool> request(PermissionType permission, BuildContext? context, {bool isShowRejectDialog = true}) async {
    if (_permissionsMap[permission] == null) return false;
    bool isGranted = false;
    if (Platform.isAndroid) isGranted = await _permissionsMap[permission]!.androidPermission.request().isGranted;
    if (Platform.isIOS) isGranted = await _permissionsMap[permission]!.iosPermission.request().isGranted;
    if (isShowRejectDialog && !isGranted) {
      _showDialog(context, _permissionsMap[permission]!.hint);
    }
    return isGranted;
  }

  static Future<PermissionStatus?> requestPermissionState(PermissionType permission, BuildContext? context,
      {bool isShowRejectDialog = true}) async {
    if (_permissionsMap[permission] == null) return null;
    PermissionStatus? permissionStatus;
    if (Platform.isAndroid) permissionStatus = await _permissionsMap[permission]!.androidPermission.request();
    if (Platform.isIOS) permissionStatus = await _permissionsMap[permission]!.iosPermission.request();
    return permissionStatus;
  }

  static Future<bool> openSetting() {
    isOpenSetting = true;
    return openAppSettings();
  }

  static void _showDialog(BuildContext? context, String hint) {
    if (context == null) return;
    defaultShowPermisionsDialogBuilder.call(context, hint: hint, rightBtnText: "开启", onClick: () async {
      Get.back();
      PermissionsUtils.isOpenSetting = await openAppSettings();
    });
  }

  static Future<bool> isPhotoPermissionGranted() {
    if (Platform.isAndroid) return Permission.storage.request().isGranted;
    if (Platform.isIOS) return Permission.photos.request().isGranted;
    return Future.value(false);
  }

  static Future<bool> isCameraPermissionGranted() {
    return Permission.camera.request().isGranted;
  }
}

class _PermissionItem {
  final Permission androidPermission;
  final Permission iosPermission;
  final String hint;

  _PermissionItem({required this.androidPermission, required this.iosPermission, required this.hint});
}
